//
//  ViewController.m
//  NewsData
//
//  Created by 3024 on 15/11/16.
//  Copyright © 2015年 ios25. All rights reserved.
//
//测试2


#import "ViewController.h"
#import "GDataXMLNode/GDataXMLNode.h"
#import "AItem.h"
#define name 1111111
#define passwork "kallen"
#define login_web @"http://knews.sinaapp.com/api-2/login.api-2.php?act=login"
#define register_web @"http://knews.sinaapp.com/api-2/register.api.php?act=register"
#define modify @"http://knews.sinaapp.com/api-2/register.api.php?act=modify"
#define comment_add @"http://knews.sinaapp.com/api-2/comment.api.php?act=add"
#define comment_del @"http://knews.sinaapp.com/api-2/comment.api.php?act=del&"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
#pragma mark - 登陆 注册 修改个人信息 评论 查看评论 删除评论
    /*
     //登陆 参数自行带入
     [self requestUrl:login_web param:@"<user><name>1111111</name><password>kallen</password></user>"];
     
     
     //注册 参数自行带入
     NSString *register_param = [NSString stringWithFormat:@"<user><name>louisgdm</name><icon>kallen.png</icon><password>q105503</password><email>a1234.1995@163.com</email><sex>男</sex><age>20</age><birthday/><regtime/><lastlogintime/></user>"];
     [self requestUrl:register_web param:register_param];
     
     //修改 参数自行带入
     NSString *modify_param = [NSString stringWithFormat:@"<user>                              <name>1111111</name><icon>kallen.png</icon><password>kallen</password><email>kallen@gmail.com</email><sex>男</sex><age>28</age><birthday/><regtime/><lastlogintime/></user>"];
     [self requestUrl:modify param:modify_param];
     
     //添加评论 参数自行带入
     NSString *comment_add_param = [NSString stringWithFormat:@"<comments><comment>Louis到此一游</comment><commenttime>2015-01-25 21:27:10</commenttime><userloc></userloc><userid>66</userid><newsid>60</newsid></comments>"];
     [self requestUrl:comment_add param:comment_add_param];
     
     
     //查看评论 参数自行带入
     
     // act：list代表获取评论列表
     // page：当前页，从0开始
     // count：每页的数量
     // newsid：具体哪条新闻id的评论
     
     NSString* comment_list_url_string = [NSString stringWithFormat:@"http://knews.sinaapp.com/api-2/comment.api.php?act=list&page=0&count=5&newsid=60"];
     [self getNewsDataWithUrl_string:comment_list_url_string];
     
     
     //删除评论 参数自行带入 id: 要删除的评论id，登陆后才能删除自己的评论
     NSString *comment_del_url_string = [NSString stringWithFormat:@"%@id=992",comment_del];
     [self getNewsDataWithUrl_string:comment_del_url_string];
     
     
     
     //获取新闻列表 参数自行带入
     NSString *getData_url_string = [NSString stringWithFormat:@"http://knews.sinaapp.com/api-2/news.api.php?act=list&page=0&count=20"];
     [self getNewsDataWithUrl_string:getData_url_string];
     
     */
    
    
    //------------------------以下解析xml--------------------------------------------------
#pragma mark - 新闻列表解析
    /*
     //新闻列表
     
     // page：当前页，从0开始
     // count：每页的数量
     
     NSData *data_xml = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://knews.sinaapp.com/api-2/news.api.php?act=list&page=0&count=20"] options:0 error:nil];
     
     GDataXMLDocument *xmlDocument = [[GDataXMLDocument alloc]initWithData:data_xml options:XML_PARSE_NOBASEFIX|XML_PARSE_NOBLANKS error:nil];
     
     //3.下面开始解析,解析应该在同一作用域中完成
     
     //3.1 从文档对象获取跟节点
     GDataXMLElement *rootElement = [xmlDocument rootElement];
     
     GDataXMLElement *dataElement = [rootElement elementsForName:@"data"].firstObject;
     //3.2 在rootElement 节点下获取所有root节点
     NSArray *arr_item_Element = [dataElement elementsForName:@"item"];
     
     
     //保存新闻的数组
     NSMutableArray *items = [[NSMutableArray alloc]initWithCapacity:arr_item_Element.count];
     //3.3 遍历
     for (GDataXMLElement *aElement in arr_item_Element) {
     GDataXMLElement *idElement = [aElement elementsForName:@"id"].firstObject;
     NSInteger newsId = idElement.stringValue.integerValue;
     //        NSLog(@"newsid;%ld",newsId);
     
     GDataXMLElement *titleElement = [aElement elementsForName:@"title"].firstObject;
     NSString *title = titleElement.stringValue;
     //        NSLog(@"title;%@",title);
     
     GDataXMLElement *imagesElement = [aElement elementsForName:@"images"].firstObject;
     NSArray *arr_imagesItem_Element = [imagesElement elementsForName:@"item"];
     //保存图片的数组
     NSMutableArray *images = [[NSMutableArray alloc]initWithCapacity:arr_imagesItem_Element.count];
     for (GDataXMLElement *imageStrElement in arr_imagesItem_Element) {
     NSString *aimageUrlStr = imageStrElement.stringValue;
     [images addObject:aimageUrlStr];
     }
     //        NSLog(@"images:%@",images);
     
     GDataXMLElement *contentElement = [aElement elementsForName:@"content"].firstObject;
     NSString *content = contentElement.stringValue;
     //解析HTML
     NSMutableString *content1 = [NSMutableString stringWithString:contentElement.stringValue];
     NSRange rang1 = [content1 rangeOfString:@"<"];
     while (rang1.location != NSNotFound) {
     NSRange rang2 = [content1 rangeOfString:@">"];
     [content1 replaceCharactersInRange:NSMakeRange(rang1.location, rang2.location-rang1.location+1) withString:@" "];
     rang1 = [content1 rangeOfString:@"<"];
     
     }
     
     content = content1;
     NSLog(@"content;%@",content);
     
     GDataXMLElement *categoryidElement = [aElement elementsForName:@"categoryid"].firstObject;
     NSInteger categoryid = categoryidElement.stringValue.integerValue;
     //        NSLog(@"categoryid;%ld",categoryid);
     
     GDataXMLElement *authoridElement = [aElement elementsForName:@"authorid"].firstObject;
     NSInteger authorid = authoridElement.stringValue.integerValue;
     //        NSLog(@"authorid;%ld",authorid);
     
     GDataXMLElement *publishtimeElement = [aElement elementsForName:@"publishtime"].firstObject;
     NSString *publishtime = publishtimeElement.stringValue;
     //        NSLog(@"publishtime;%@",publishtime);
     
     AItem *item = [AItem aItemWithNewsid:newsId title:title content:content categoryid:categoryid authorid:authorid publishtime:publishtime];
     item.images = images;
     }
     
     NSLog(@"items:%@",items);
     */
#pragma mark - 图片新闻解析
    /*
     //图片新闻
     
     // act：imgNewsList代表获取图片列表新闻
     // page：当前页，从0开始
     // count：每页的数量
     // cid：代表图片新闻
     
     NSData *data_xml = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://knews.sinaapp.com/api-2/news.api.php?act=imgNewsList&page=0&count=3&cid=3"] options:0 error:nil];
     
     GDataXMLDocument *xmlDocument = [[GDataXMLDocument alloc]initWithData:data_xml options:XML_PARSE_NOBASEFIX|XML_PARSE_NOBLANKS error:nil];
     
     //3.下面开始解析,解析应该在同一作用域中完成
     
     //3.1 从文档对象获取跟节点
     GDataXMLElement *rootElement = [xmlDocument rootElement];
     
     GDataXMLElement *dataElement = [rootElement elementsForName:@"data"].firstObject;
     //3.2 在rootElement 节点下获取所有root节点
     NSArray *arr_item_Element = [dataElement elementsForName:@"item"];
     
     
     //保存新闻的数组
     NSMutableArray *items = [[NSMutableArray alloc]initWithCapacity:arr_item_Element.count];
     //3.3 遍历
     for (GDataXMLElement *aElement in arr_item_Element) {
     GDataXMLElement *idElement = [aElement elementsForName:@"id"].firstObject;
     NSInteger newsId = idElement.stringValue.integerValue;
     NSLog(@"newsid;%ld",newsId);
     
     GDataXMLElement *titleElement = [aElement elementsForName:@"title"].firstObject;
     NSString *title = titleElement.stringValue;
     NSLog(@"title;%@",title);
     
     GDataXMLElement *contentElement = [aElement elementsForName:@"content"].firstObject;
     NSString *content = contentElement.stringValue;
     //解析HTML
     NSMutableString *content1 = [NSMutableString stringWithString:contentElement.stringValue];
     NSRange rang1 = [content1 rangeOfString:@"<"];
     while (rang1.location != NSNotFound) {
     NSRange rang2 = [content1 rangeOfString:@">"];
     [content1 replaceCharactersInRange:NSMakeRange(rang1.location, rang2.location-rang1.location+1) withString:@" "];
     rang1 = [content1 rangeOfString:@"<"];
     
     }
     content = content1;
     NSLog(@"content;%@",content);
     
     GDataXMLElement *categoryidElement = [aElement elementsForName:@"categoryid"].firstObject;
     NSInteger categoryid = categoryidElement.stringValue.integerValue;
     NSLog(@"categoryid;%ld",categoryid);
     
     GDataXMLElement *authoridElement = [aElement elementsForName:@"authorid"].firstObject;
     NSInteger authorid = authoridElement.stringValue.integerValue;
     NSLog(@"authorid;%ld",authorid);
     
     GDataXMLElement *publishtimeElement = [aElement elementsForName:@"publishtime"].firstObject;
     NSString *publishtime = publishtimeElement.stringValue;
     NSLog(@"publishtime;%@",publishtime);
     
     GDataXMLElement *imagesElement = [aElement elementsForName:@"pics"].firstObject;
     NSArray *arr_imagesItem_Element = [imagesElement elementsForName:@"item"];
     //保存图片的数组
     NSMutableArray *images = [[NSMutableArray alloc]initWithCapacity:arr_imagesItem_Element.count];
     for (GDataXMLElement *imageStrElement in arr_imagesItem_Element) {
     NSString *aimageUrlStr = imageStrElement.stringValue;
     [images addObject:aimageUrlStr];
     }
     NSLog(@"images:%@",images);
     
     GDataXMLElement *infoElement = [aElement elementsForName:@"info"].firstObject;
     NSArray *arr_infoItem_Element = [infoElement elementsForName:@"item"];
     //保存info的数组
     NSMutableArray *infos = [[NSMutableArray alloc]initWithCapacity:arr_infoItem_Element.count];
     for (GDataXMLElement *infoStrElement in arr_infoItem_Element) {
     NSString *aInfo = infoStrElement.stringValue;
     [infos addObject:aInfo];
     }
     NSLog(@"infos:%@",infos);
     
     
     AItem *item = [AItem aItemWithNewsid:newsId title:title content:content categoryid:categoryid authorid:authorid publishtime:publishtime];
     item.images = images;
     item.infos = infos;
     [items addObject:item];
     
     }
     
     NSLog(@"items:%@",items);
     */
#pragma mark - 视频新闻解析
    ///*
    //视频新闻
    
    // act：videoList代表获取视频列表新闻
    // page：当前页，从0开始
    // count：每页的数量
    // cid：代表视频新闻
    
    NSData *data_xml = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://knews.sinaapp.com/api-2/news.api.php?act=videoList&page=0&count=2&cid=4"] options:0 error:nil];
    
    GDataXMLDocument *xmlDocument = [[GDataXMLDocument alloc]initWithData:data_xml options:XML_PARSE_NOBASEFIX|XML_PARSE_NOBLANKS error:nil];
    
    //3.下面开始解析,解析应该在同一作用域中完成
    
    //3.1 从文档对象获取跟节点
    GDataXMLElement *rootElement = [xmlDocument rootElement];
    
    GDataXMLElement *dataElement = [rootElement elementsForName:@"data"].firstObject;
    //3.2 在rootElement 节点下获取所有root节点
    NSArray *arr_item_Element = [dataElement elementsForName:@"item"];
    
    
    //保存新闻的数组
    NSMutableArray *items = [[NSMutableArray alloc]initWithCapacity:arr_item_Element.count];
    NSMutableArray *images = [NSMutableArray array];
    //3.3 遍历
    for (GDataXMLElement *aElement in arr_item_Element) {
        GDataXMLElement *idElement = [aElement elementsForName:@"id"].firstObject;
        NSInteger newsId = idElement.stringValue.integerValue;
        //NSLog(@"newsid;%ld",newsId);
        
        GDataXMLElement *titleElement = [aElement elementsForName:@"title"].firstObject;
        NSString *title = titleElement.stringValue;
        //NSLog(@"title;%@",title);
        
        GDataXMLElement *contentElement = [aElement elementsForName:@"content"].firstObject;
        NSString *content = contentElement.stringValue;
        
        //先获取视频中的一张图片
        NSRange rang_httpH = [content rangeOfString:@"http://knews-upload.stor.sinaapp.com/ueditor/php/upload/image/"];
        if(rang_httpH.location != NSNotFound)
        {
            NSRange rang_httpF = [content rangeOfString:@".png"];
            NSString *imageStr = [content substringWithRange:NSMakeRange(rang_httpH.location, rang_httpF.location+4 - rang_httpH.location)];
            NSLog(@"imageStr:%@",imageStr);
            [images addObject:imageStr];
        }else
        {

        }
        
        //解析HTML
        NSMutableString *content1 = [NSMutableString stringWithString:contentElement.stringValue];
        
        NSRange rang1 = [content1 rangeOfString:@"<"];
        while (rang1.location != NSNotFound) {
            NSRange rang2 = [content1 rangeOfString:@">"];
            [content1 replaceCharactersInRange:NSMakeRange(rang1.location, rang2.location-rang1.location+1) withString:@""];
            rang1 = [content1 rangeOfString:@"<"];
            
        }
        
        content = content1;
        
        NSLog(@"content;%@",content);
        
        GDataXMLElement *categoryidElement = [aElement elementsForName:@"categoryid"].firstObject;
        NSInteger categoryid = categoryidElement.stringValue.integerValue;
        //NSLog(@"categoryid;%ld",categoryid);
        
        GDataXMLElement *authoridElement = [aElement elementsForName:@"authorid"].firstObject;
        NSInteger authorid = authoridElement.stringValue.integerValue;
        //NSLog(@"authorid;%ld",authorid);
        
        GDataXMLElement *publishtimeElement = [aElement elementsForName:@"publishtime"].firstObject;
        NSString *publishtime = publishtimeElement.stringValue;
        //NSLog(@"publishtime;%@",publishtime);
        
        GDataXMLElement *videourlElement = [aElement elementsForName:@"videourl"].firstObject;
        NSString *videourl = videourlElement.stringValue;
        //NSLog(@"videourl:%@",videourl);
        
        
        AItem *item = [AItem aItemWithNewsid:newsId title:title content:content categoryid:categoryid authorid:authorid publishtime:publishtime];
        item.videourl = videourl;
        item.images = images;
        NSLog(@"item.images.firstObject:%@",item.images.firstObject);
        [items addObject:item];
        
    }
    
    NSLog(@"items:%@",items);
    //*/
    
}

-(void)requestUrl:(NSString*)url_string param:(NSString*)param
{
    //创建请求对象
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url_string]];//默认为get请求
    request.timeoutInterval=5.0;//设置请求超时为5秒
    request.HTTPMethod=@"post";//设置请求方法
    
    //把拼接后的字符串转换为data，设置请求体
    request.HTTPBody=[param dataUsingEncoding:NSUTF8StringEncoding];
    
    //设置请求头
    [request addValue:@"xml" forHTTPHeaderField:@"Content-Type"];
    
    //发送请求
    
    //4.task
    
    NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        //        NSLog(@"datastr:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        //        NSLog(@"response:%@",response);
        //        NSLog(@"error:%@",error);
    }];
    
    //5.执行
    [task resume];
}

-(void)getNewsDataWithUrl_string:(NSString*)url_string
{
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url_string]];
    
    NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSString* request_string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"request_string:%@",request_string);
    }];
    [task resume];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
